#ifndef SERIAL_H
#define SERIAL_H

#include <string>

class Serial
{
public:
    Serial(const char *_pszDeviceName, int _iBaud);
    virtual ~Serial();

    /// read _iBufSize bytes into _pBuf
    virtual int read(void *_pBuf, int _iBufSize);

    /// reads one line of text (times out if no characters are received in _iTimeoutMs)
    virtual std::string readln(int _iTimeoutMs);

    /// writes given buffer
    virtual int write(const void *_pBuf, int _iSize);

    /// writes one byte
    virtual int write(char _ch);

    /// flush pending data
    virtual void flush();

    /// returns false if serial connection failed
    virtual bool operational() const {return m_strDeviceError.length() == 0;}

    /// returns the error description when the connection failed
    virtual std::string error() const {return m_strDeviceError;}

private:
    Serial();
    Serial(const Serial &);
    Serial &operator=(const Serial &);

    /// opens the serial connection
    virtual void init(const char *_pszDeviceName, int _iBaud);

private:
    std::string     m_strDeviceName;
    std::string     m_strDeviceError;
    int             m_iBaud;
    int             m_hSerialDevice;
};

#endif // SERIAL_H
